<template>
  <div id="add" @click="addevent" v-if="addUrl !== ''">
      <img :style="{height:heightpx + 'px'}" :src="addUrl"/>
  </div>
</template>
<script>
import api from '../api'
import utils from '../utils'
export default {
  props: ['addposition'],
  data () {
    return {
      addimg: {},
      heightpx: 0,
      addUrl: '',
      tokenval: ''
    }
  },
  created () {
    var timeStamp = String(Number(new Date()))
    this.tokenval = utils.AnonToken(timeStamp)
    var params = {
      Tokenuser: this.tokenval,
      userid: this.$store.state.userID || '',
      timestamp: timeStamp,
      addtype: this.addposition
    }
    api.ads(params).then(res => {
      console.log(res)
      if (res.status === 200) {
        this.addimg = res.data
        if (res.data) {
          this.heightpx = res.data.height
          this.addUrl = res.data.picPath
        }
      }
    })
  },
  methods: {
    addevent () {
      if (this.addimg.linkType === 0) {
        window.location.href = this.addimg.linkUrl
      } else if (this.addimg.linkType === 1) {
        window.location.href = this.addimg.linkUrl + '&userId=' + this.$store.state.userID
      }
    }
  }
}
</script>

